Option Public

Dim s As NotesSession
Dim db As NotesDatabase
Dim profileView As NotesView
Dim systemprofileDoc As NotesDocument
Dim MsgText As String

Sub Initialize
	
	Set s = New NotesSession
	Set db = s.CurrentDatabase
	Set profileView = db.GetView("Profile")
	Set systemprofileDoc = profileView.GetDocumentByKey("Profile",True)
	
End Sub

Sub ChangeStatus ( StatusValue As String, PersonField As String, DateField As String )
	
	Dim w As New NotesUIWorkspace     
	Dim uidoc As NotesUIDocument    
	Dim doc As NotesDocument	
	Dim item As NotesItem
	Dim timestamp As New NotesDateTime( "" )	
	Dim LogEntry As String	
	Set uidoc = w.CurrentDocument	
	Set doc = uidoc.Document
	Dim person As New NotesName(s.UserName)   
	
	'-------------------------------------------------------------------------
	' Set the status field to the new value
	'-------------------------------------------------------------------------
	Call CheckEditMode	
	doc.Status = StatusValue
	
	'-------------------------------------------------------------------------
	' Update any additional fields, do nothing if field ""
	'-------------------------------------------------------------------------
	Call timestamp.SetNow
	Set item = doc.ReplaceItemValue( PersonField, person.Abbreviated )
	Set item = doc.ReplaceItemValue( DateField, timestamp.LocalTime )
	
	'-------------------------------------------------------------------------
	' Append a log entry to the history field
	'-------------------------------------------------------------------------
	LogEntry="("+Cstr(Now)+") - " + person.Common + " - " + "Status changed to " + StatusValue
	Set item = doc.GetFirstItem("History")
	Call item.AppendToTextList( LogEntry )
	
End Sub

Function CheckFieldValues ( DocRef As NotesDocument )
	
	CheckFieldValues = True
	MsgText = ""
	
	If Trim(DocRef.RequestTitle(0)) = "" Then
		MsgText = MsgText + "Specify a title for the request." + Chr$(13)
	End If		
	
	If MsgText <> "" Then
		Msgbox MsgText, 16, "Required Fields."
		CheckFieldValues = False
	End If	
	
End Function

Function CheckMsgFieldValues ( DocRef As NotesDocument )
	
	CheckMsgFieldValues = True
	MsgText = ""
	
	If Trim(DocRef.SendTo(0)) = "" Then
		MsgText = MsgText + "Specify an email in the SendTo field." + Chr$(13)
	End If		
	
	If Trim(DocRef.Subject(0)) = "" Then
		MsgText = MsgText + "Specify an email Subject." + Chr$(13)
	End If		
	
	If MsgText <> "" Then
		Msgbox MsgText, 16, "Required Fields."
		CheckMsgFieldValues = False
	End If	
	
End Function

Sub CheckEditMode
	
	Dim w As New NotesUIWorkspace     
	Dim uidoc As NotesUIDocument
	Set uidoc = w.CurrentDocument     
	If uidoc.EditMode = False Then
		Call w.EditDocument( True )         
	End If
	
End Sub

Sub SendNotice ( NotifyMsg As String, DocRef As NotesDocument )
	
	Dim rtitem As NotesRichTextItem
	Dim rtitemB As NotesRichTextItem
	Dim doc As NotesDocument  		
	Dim msgDoc As NotesDocument
	Dim view As NotesView
	Dim key As Variant
	Dim Line1 As String
	Dim Line2 As String
	Dim Line3 As String
	Dim Line4 As String
	Dim Line5 As String
	Dim Line6 As String
	
	'-----------------------------------------------------------------------
	' Check to see if emails are permitted
	'-----------------------------------------------------------------------
	If systemprofileDoc.AllowNotification(0) = "Yes" Then 
		
		' Set the default line statements for body of email
		line1 = "Request Title:       	" + DocRef.RequestTitle(0)
		line2 = "Request By:           	" + DocRef.RequestBy(0)
		line3 = "Request Date:       	" + DocRef.CreateDate(0)
		line4 = "Request Status:     	" + DocRef.Status(0)
		line5 = "Click here to open the document --> "	
		line6 = "Click here to open the database  --> "	
		
		'-----------------------------------------------------------------------
		'Lookup up which message to send out		
		'-----------------------------------------------------------------------
		key = systemprofileDoc.GetItemValue( NotifyMsg )		
		
		Forall x In key			
			'-----------------------------------------------------------------------
			' Get the message text information
			'-----------------------------------------------------------------------
			Set View = db.GetView("(MsgID)")
			Set MsgDoc = View.GetDocumentByKey( Cint( x ) ,True)
			
			'-----------------------------------------------------------------------
			'Generate email and send notice 
			'-----------------------------------------------------------------------
			Set doc = New NotesDocument( db )
			doc.Form = "Memo"	
			doc.SendTo = ReplaceElement ( msgDoc.SendTo, "AUTHOR", DocRef)
			doc.CopyTo =  ReplaceElement ( msgDoc.SendCC, "AUTHOR", DocRef)
			doc.BlindCopyTo = ReplaceElement ( msgDoc.SendBCC, "AUTHOR", DocRef)
			doc.Subject = msgDoc.Subject(0)
			Set rtitem = New NotesRichTextItem( doc, "Body" )
			Call rtitem.AddNewline(1)
			Set rtitemB = msgDoc.GetFirstItem( "Body" )
			Call rtitem.AppendRTItem( rtitemB )		
			Call rtitem.AddNewline(2)
			Call rtitem.AppendText( line1 ) 
			Call rtitem.AddNewline(1)
			Call rtitem.AppendText( line2 ) 
			Call rtitem.AddNewline(1)
			Call rtitem.AppendText( line3 ) 
			Call rtitem.AddNewline(1)
			Call rtitem.AppendText( line4 ) 
			Call rtitem.AddNewline(2)
			Call rtitem.AppendText( line5 )
			Call rtitem.AppendDocLink( DocRef,  db.Title )
			Call rtitem.AddNewline(1)
			Call rtitem.AppendText( line6 )
			Call rtitem.AppendDocLink( db,  db.Title )
			Call rtitem.AddNewline(2)
			Call rtitem.AppendText(  "MessageID: " + x )
			doc.Send False		
		End Forall
	End If
	
End Sub

Function ReplaceElement ( varFromList As Variant, varTarget As Variant, DocRef As NotesDocument) As Variant
	
	Redim ReturnArray(0) As String
	Dim i As Integer
	i = 0 
	Forall varElement In varFromList
		Redim Preserve ReturnArray (i) As String
		If varElement = varTarget Then
			ReturnArray(i) = DocRef.RequestBy(0)
		Else
			ReturnArray(i) = varElement
		End If
		i = i + 1		
	End Forall
	ReplaceElement = ReturnArray
	
End Function
